package com.nxhz.felix;

import com.nxhz.felix.server.Hello;
import com.nxhz.felix.server.impl.IHelloImpl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jierui
 * @package com.nxhz.felix
 * @date 2022/7/13-9:51
 * @description:
 */
public class ServerBundleActivator implements BundleActivator {


    private List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>();

    private static BundleContext context;

    static BundleContext getContext() {
        return context;
    }

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        ServerBundleActivator.context = bundleContext;
        System.out.println("----------------server start---------------------");
        //注册hello接口中的服务
        registrations.add(bundleContext
                .registerService(Hello.class.getName(), new IHelloImpl(), null));
        System.out.println("----------------server end---------------------");
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        ServerBundleActivator.context = null;
        for (ServiceRegistration registration : registrations) {
            System.out.println("unregistering: " + registration);
            registration.unregister();
        }
    }
}

